package com.cj.peiwan.server.other;

import com.google.gson.reflect.TypeToken;
import org.wei.utils.IOUtil;
import org.wei.utils.exception.WrongDateFormatException;
import ytx.org.apache.http.HttpEntity;
import ytx.org.apache.http.HttpResponse;
import ytx.org.apache.http.client.ClientProtocolException;
import ytx.org.apache.http.client.methods.HttpPost;
import ytx.org.apache.http.client.methods.HttpPut;
import ytx.org.apache.http.entity.StringEntity;
import ytx.org.apache.http.impl.client.DefaultHttpClient;
import ytx.org.apache.http.impl.client.SystemDefaultHttpClient;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

public class HuanxinSDK {
    private static HuanxinSDK instance;
    private String baseUrl = String.format("https://a1.easemob.com/%s/%s/", "chuangjike", "peiwan");
    private String cacheToken;
    private long lastCacheTime;

    private HuanxinSDK() {
    }

    public synchronized static HuanxinSDK getInstance() {
        if (instance == null) {
            instance = new HuanxinSDK();
        }
        return instance;
    }

    public static void main(String[] args) throws WrongDateFormatException, ParseException {

    }


    public String getToken() throws IOException {
        if (System.currentTimeMillis() - lastCacheTime < 1184000) {
            return cacheToken;
        }
        HttpPost httpPost = new HttpPost(baseUrl + "token");
        Map<String, Object> map = new HashMap<>();
        map.put("grant_type", "client_credentials");
        map.put("client_id", "YXA6iEWtAALXEeWwct-PMHLMGg");
        map.put("client_secret", "YXA6Z4lRc8-SEKntiA1k7c-bTn3TS5Y");
        httpPost.setEntity(new StringEntity(Global.getGson().toJson(map)));
        DefaultHttpClient httpClient = new SystemDefaultHttpClient();
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity entity = httpResponse.getEntity();
        InputStream inputStream = entity.getContent();
        String res = IOUtil.inStream2String(inputStream, "utf-8");
        Map<String, Object> map2 = Global.getGson().fromJson(res, new TypeToken<HashMap<String, Object>>() {
        }.getType());
        cacheToken = (String) map2.get("access_token");
        lastCacheTime = System.currentTimeMillis();
        return cacheToken;
    }

    public String register(String username, String password, String nickname) throws IOException {
        HttpPost httpPost = new HttpPost(baseUrl + "users");
        Map<String, Object> map = new HashMap<>();
        map.put("username", username);
        map.put("password", password);
        map.put("nickname", nickname);
        httpPost.setHeader("Authorization", "Bearer " + getToken());
        httpPost.setEntity(new StringEntity(Global.getGson().toJson(map)));
        DefaultHttpClient httpClient = new SystemDefaultHttpClient();
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity entity = httpResponse.getEntity();
        InputStream inputStream = entity.getContent();
        String res = IOUtil.inStream2String(inputStream, "utf-8");
        return res;
    }

    public boolean setPassword(String username, String newPassword) throws IOException {
        HttpPut httpPut = new HttpPut(baseUrl + "users" + "/" + username + "/password");
        Map<String, Object> map = new HashMap<>();
        map.put("newpassword", newPassword);
        httpPut.setHeader("Authorization", "Bearer " + getToken());
        httpPut.setEntity(new StringEntity(Global.getGson().toJson(map)));
        DefaultHttpClient httpClient = new SystemDefaultHttpClient();
        HttpResponse httpResponse = httpClient.execute(httpPut);
        return httpResponse.getStatusLine().getStatusCode() == 200;
    }

    public boolean setNickname(String username, String newNickname) throws IOException {
        HttpPut httpPut = new HttpPut(baseUrl + "users" + "/" + username);
        Map<String, Object> map = new HashMap<>();
        map.put("nickname", newNickname);
        httpPut.setHeader("Authorization", "Bearer " + getToken());
        httpPut.setEntity(new StringEntity(Global.getGson().toJson(map)));
        DefaultHttpClient httpClient = new SystemDefaultHttpClient();
        HttpResponse httpResponse = httpClient.execute(httpPut);
        return httpResponse.getStatusLine().getStatusCode() == 200;
    }

    public boolean sendMessage(String from, String to, String content) {
        boolean r = false;
        try {
            content = new String(content.getBytes(), "iso-8859-1");
            HttpPost httpPost = new HttpPost(baseUrl + "messages");
            Map<String, Object> map = new HashMap<>();
            map.put("target_type", "users");
            String[] usernames = {to};
            map.put("target", usernames);
            map.put("from", from);
            Map<String, String> msg = new HashMap<>();
            msg.put("type", "txt");
            msg.put("msg", content);
            map.put("msg", msg);
            httpPost.setHeader("Authorization", "Bearer " + getToken());
            httpPost.setEntity(new StringEntity(Global.getGson().toJson(map)));
            DefaultHttpClient httpClient = new SystemDefaultHttpClient();
            HttpResponse httpResponse = httpClient.execute(httpPost);
            r = httpResponse.getStatusLine().getStatusCode() == 200;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return r;
    }

}
